package com.douqu.game.main.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.bean.core.util.MD5Utils;
import com.douqu.game.core.config.RechargeConfig;
import com.douqu.game.core.e.E_ServerTaskType;
import com.douqu.game.core.e.ReturnMessage;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.HttpFactory;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.web.response.BaseResponseDto;
import com.douqu.game.core.web.response.ErrorResponseDto;
import com.douqu.game.core.web.response.SuccessResponseDto;
import com.douqu.game.main.GameServer;
import com.douqu.game.main.server.UpdateManager;
import com.douqu.game.main.server.entity.ServerTask;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author: miracle.
 * Description:
 * Date: 2018/4/17 0017 下午 3:54
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
@RestController
public class ManagerController {

    @RequestMapping(value = "/manager/test", produces = "application/json;charset=UTF-8")
    public BaseResponseDto test(String password)
    {
        LogUtils.info("http test ==================password:" + password);

        return new BaseResponseDto(ReturnMessage.SUCCESS);
    }

    @RequestMapping(value = "/manager/getGoods",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public BaseResponseDto<JSONObject> getGoods() throws IOException{
        try{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("propConfig",DataFactory.getInstance().getBaseDataToJson(DataFactory.PROP_KEY));
            jsonObject.put("assetConfig",DataFactory.getInstance().getBaseDataToJson(DataFactory.ASSET_KEY));
            jsonObject.put("cardConfig",DataFactory.getInstance().getBaseDataToJson(DataFactory.CARD_KEY));
            return new SuccessResponseDto(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return new BaseResponseDto(ReturnMessage.FAILURE);
        }
    }

    /**
     * 获取
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/manager/getRecharge",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public BaseResponseDto<JSONObject> getRecharge() throws IOException{
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("rechargeConfig",DataFactory.getInstance().getDetailDataToJson(DataFactory.RECHARGE_KEY));
            return new SuccessResponseDto(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return new BaseResponseDto(ReturnMessage.FAILURE);
        }
    }


//    @RequestMapping(value = HttpFactory.MANAGER_GET_RECHARGE_MONEY)
//    public BaseResponseDto getRechargeId(String productId)
//    {
//        if(Utils.isNullOrEmpty(productId))
//            return null;
//
//        List<RechargeConfig> list = DataFactory.getInstance().getDataList(DataFactory.RECHARGE_KEY);
//        if(list != null)
//        {
//            for(RechargeConfig rechargeConfig : list)
//            {
//                if(rechargeConfig.asProductId.equals(productId))
//                {
//                    return new SuccessResponseDto(new JSONObject().fluentPut("diamond", rechargeConfig.getDiamond()).fluentPut("rmb", rechargeConfig.rmb).fluentPut("rechargeId", rechargeConfig.id));
//                }
//            }
//        }
//        return null;
//    }


    @RequestMapping(value = HttpFactory.MANAGER_GET_RECHARGE_MONEY)
    public BaseResponseDto getRechargeId(Integer rechargeId)
    {
        if(rechargeId == null)
            return null;

        RechargeConfig rechargeConfig = DataFactory.getInstance().getGameObject(DataFactory.RECHARGE_KEY, rechargeId);
        if(rechargeConfig != null)
            return new SuccessResponseDto(new JSONObject().fluentPut("diamond", rechargeConfig.getDiamond()).fluentPut("rmb", rechargeConfig.rmb));

        return null;
    }



    @RequestMapping(value = "/manager/9a4efc24297995253168e9f962c8474f")
    public BaseResponseDto closeServer(HttpServletRequest request, String password)
    {
        String host = request.getRemoteHost();
//        String addr = request.getRemoteAddr();
        if(host.indexOf("127.0.0.1") == -1 && host.indexOf("localhost") == -1)
        {
            LogUtils.error("关服操作只允许在本机操作!");
            return new ErrorResponseDto(ReturnMessage.ILLEGAL);
        }

        String str = com.bean.core.util.TimeUtils.getCurrentTime("yyyyMMddHH") + "80932817" + GameServer.getInstance().getServerId();
        String localPwd = MD5Utils.encode(str);
        if(!localPwd.equals(password))
        {
            LogUtils.error("非法请求关服操作 password:" + password + " localPwd:" + localPwd);
            return new ErrorResponseDto(ReturnMessage.ILLEGAL);
        }

        LogUtils.warn("准备关服了!");

        new Thread(()->{
            GameServer.getInstance().stop();
        }).start();


        return new SuccessResponseDto();
    }



    @RequestMapping(value = "/manager/addServerTask")
    public BaseResponseDto addServerTask(Integer id, Integer type, Long startTime, String password)
    {
        if(id == null || type == null || startTime == null)
        {
            LogUtils.warn("收到发来的添加计划任务消息参数错误1");
            return new ErrorResponseDto(ReturnMessage.PARAM_ERROR);
        }
        E_ServerTaskType taskType = E_ServerTaskType.forNumber(type);
        if(taskType == null)
        {
            LogUtils.warn("收到发来的添加计划任务消息参数错误2");
            return new ErrorResponseDto(ReturnMessage.PARAM_ERROR);
        }

        String str = com.bean.core.util.TimeUtils.getCurrentTime("yyyyMMddHH") + "80932817" + GameServer.getInstance().getServerId();
        String localPwd = MD5Utils.encode(str);
        if(!localPwd.equals(password))
        {
            LogUtils.error("非法请求addServerTask password:" + password + " localPwd:" + localPwd);
            return new ErrorResponseDto(ReturnMessage.ILLEGAL);
        }

        ServerTask serverTask = new ServerTask(id, taskType, startTime);
        LogUtils.info("收到发来的新增计划任务消息 -> serverTask:" + serverTask);
        UpdateManager.getInstance().addServerTask(serverTask);
        return null;
    }

    @RequestMapping(value = "/manager/delServerTask")
    public BaseResponseDto delServerTask(Integer id, String password)
    {
        if(id == null)
        {
            LogUtils.warn("收到发来的删除计划任务消息参数错误");
            return new ErrorResponseDto(ReturnMessage.PARAM_ERROR);
        }

        String str = com.bean.core.util.TimeUtils.getCurrentTime("yyyyMMddHH") + "80932817" + GameServer.getInstance().getServerId();
        String localPwd = MD5Utils.encode(str);
        if(!localPwd.equals(password))
        {
            LogUtils.error("非法请求delServerTask password:" + password + " localPwd:" + localPwd);
            return new ErrorResponseDto(ReturnMessage.ILLEGAL);
        }

        LogUtils.info("收到发来的删除计划任务消息 -> id:" + id);
        UpdateManager.getInstance().removeServerTask(id);
        return null;
    }



    @RequestMapping(value = "/manager/whiteNotify")
    public BaseResponseDto whiteNotify()
    {
        LogUtils.warn("收到更新白名单的通知");
        GameServer.getInstance().getWorldManager().updateWhite();
        return null;
    }
}
